package hanxx.test.one;

import com.sun.istack.NotNull;
import hanxx.test.one.domain.Dept1;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @edit:韩鑫鑫
 * @Date: 2022/11/18/11:51
 * @Description:
 */
public class StudySteam {

    // 此处及下面是steam流的测试
    public static void main(String[] args) {
        /** * 【常规方式】 * 从给定句子中返回单词长度大于5的单词列表，按长度倒序输出，最多返回3个 * * @param sentence 给定的句子，约定非空，且单词之间仅由一个空格分隔 * @return 倒序输出符合条件的单词列表 */
        /*String ss = "good";
        System.out.println(ss.length());
        String test = "hsafdfd gger s tsafrwaf eregr qwer fsf sfasser erter tgdg reter ggsdfgdhre wtwe r fsdg ertwet";
        List<String> strings = sortTest(test);
        strings.stream()
                .skip(2)
                .forEach(word -> System.out.println(word+"~~~~~~~~~~~~~~~~~~~"));
        for (String string : strings) {
            System.out.println(string);
        }*/
//        stringToIntMap();
//        stringToIntFlatmap();
//        testGetTargetUsers();
//        testSimpleStopOptions();


        wheelTime();

    }
    //学习
    public static void wheelTime(){
        Timer timer = new HashedWheelTimer();
        timer.newTimeout(timeout -> System.out.println("5s后执行:"+timeout),5, TimeUnit.SECONDS);
        timer.newTimeout(timeout -> System.out.println("10s后执行"),10,TimeUnit.SECONDS);
        timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("哈哈哈哈哈");
            }
        },13,TimeUnit.SECONDS);

        System.out.println("主线程结束");
    }

    //简单的终结函数
    public static void testSimpleStopOptions() {
        List<String> ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");
        // 统计stream操作后剩余的元素个数
        System.out.println(ids.stream().filter(s -> s.length() > 2).count());
        // 判断是否有元素值等于205
        System.out.println(ids.stream().filter(s -> s.length() > 2).anyMatch("205"::equals));
        // findFirst操作
        ids.stream().filter(s -> s.length() > 2)
                .findFirst()
                .ifPresent(s -> System.out.println("findFirst:" + s));
    }

    //常见中间操作函数
    public static void testGetTargetUsers() {
        List<String> ids = Arrays.asList("205","10","308","49","627","193","111", "193");
        // 使用流操作
        List<Dept1> results = ids.stream()
                .filter(s -> s.length() > 2)
                .distinct()
                .map(Integer::valueOf)
                .sorted(Comparator.comparingInt(o -> o))
                .limit(3)
                .map(id -> new Dept1(id))
                .peek(wor -> System.out.println(wor))
                .collect(Collectors.toList());
    }

    /** * 演示map的用途：一对一转换 将String数据类型转为Integer类型 */
    public static void stringToIntMap() {
        List<String> ids = Arrays.asList("205","105","308","469","627","193","111");
        // 使用流操作
        List<Integer> collect = ids.stream()
                .map(s -> Integer.valueOf(s))
                .peek(word -> System.out.println(word))
                .collect(Collectors.toList());
    }

    /** 演示flatMap的用途：一对多转换 将单词句子转换为多个单独的单词 单词句子如果是多个则会分别拆分，然后合并起来 */
    public static void stringToIntFlatmap() {
        List<String> sentences = Arrays.asList("hello world","Jia Gou Wu Dao");
        // 使用流操作
        sentences.stream()
                .flatMap(sentence -> Arrays.stream(sentence.split(" ")))
                .forEach(word -> System.out.println(word));
    }

    //等价与下面的方法
    public static List<String> streamTest(String s){
        return Arrays.stream(s.split(" "))
                .filter(word -> word.length() > 5)
                .sorted((o1, o2) -> o2.length() - o1.length())
                .limit(3)
                .collect(Collectors.toList());
    }

    public static List<String> sortTest(@NotNull String sentence) {
        // 先切割句子，获取具体的单词信息
        String[] words = sentence.split(" ");
        List<String> wordList = new ArrayList<>();
        // 循环判断单词的长度，先过滤出符合长度要求的单词
        for (String word : words) {
            if (word.length() > 5) {
                wordList.add(word);
            }
        }
        // 对符合条件的列表按照长度进行排序
        wordList.sort((o1, o2) -> o2.length() - o1.length());
        // 判断list结果长度，如果大于3则截取前三个数据的子list返回
        if (wordList.size() > 3) {
            wordList = wordList.subList(0, 3);
        }
        return wordList;
    }
}
